preciso
Precise Mathematical Functions
features:
- no floating point arithmetic errors
- relatively fast
- functional programming (no class methods to learn)
- simple input and output (simple numerical strings)
- import only what you need
- tested on ten thousand random numbers
- supports scientific exponential notation
- supports CJS and ESM
- supports TypeScript
- imaginary numbers
basic usage
The following functions are supported:
install
You typically want to install preciso using the NPM CLI, pnpm, yarn, or something similar.
npm install preciso
If you don't have access to a bundler, you can "install" preciso using an HTML script tag,
which will set a global preciso variable.
<script src="https://unpkg.com/preciso"></script>
<script>
console.log(preciso.abs("-10"));
</script>
limitations
- limited support for
Infinity
and -Infinity
- no support for
NaN
absolute
import abs from "preciso/absolute.js";
abs("-10");
"10"
add
Add two numbers together.
import add from "preciso/add.js";
add("0.1", "0.2");
"0.3"
add("Infinity", "0.1");
"Infinity"
add("Infinity", "-Infinity");
"NaN"
add("Infinity", "-Infinity", { infinity_minus_infinity: null });
null
binomial_coefficient
import binomial_coefficient from "preciso/binomial_coefficient.js";
binomial_coefficient("7", "3")
"35"
compare
import compare from "preciso/compare.js";
compare("0.1", "0.2");
"<"
compare("0.1", "-0.2");
">"
compare("0.1", "+.1");
"="
ceil
import ceil from "preciso/ceil.js";
ceil(46);
"46"
ceil(-7.000000000000000000000000000000000000004);
"-7"
cube root
import cube_root from "preciso/cube_root.js";
cube_root("8");
divide
import divide from "preciso/divide.js";
divide("-714.7008086132632", "8135.725531");
'-0.0878472123832102762218908980055167989417759034280282678823325216230183564682007707223868489179001533'
euler's number
Calculate Euler's Number, also known as e.
import e from "preciso/eulers_number.js";
e();
'2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274'
e({ max_decimal_digits: 10 });
'2.7182818286'
exp
Raise Euler's Number to the given power
import exp from "preciso/exp.js";
exp("-Infinity")
exp("-1")
exp("0")
exp("1")
exp("2")
exp("10")
exp("Infinity")
factorial
import factorial from "preciso/factorial.js";
factorial("0");
"1"
factorial("3");
"6"
factorial("10");
"3628800"
flip_sign
import flip_sign from "preciso/flip_sign.js";
flip_sign(10);
-10
flip_sign(-99)
99
flip_sign(0)
0
floor
import floor from "preciso/floor.js";
floor("45.9523663245126542371265437612538671523854172437561258367152356412734512");
"45"
floor("-45.95126753876125376512437641236512473654126345126534712653476152437651243");
"-46"
mean
import mean from "preciso/mean.js";
mean(["1", "10", "4"])
"5"
mean(["1", "2", "8"], { max_decimal_digits: 3 })
"3.667"
mean(["1", "2", "8"], { ellipsis: true })
"3.666..."
hypotenuse
Calculate Hypotenuse in two or more dimensions. This was inspired by Math.hypot.
import hypotenuse from "preciso/hypotenuse.js";
hypotenuse("3", "4")
"5"
hypotenuse("3", "4", "5", { max_decimal_digits: 10 })
"7.0710678118"
hypotenuse("3i", "4i")
"5"
max
import max from "preciso/min.js";
max("0.1", "0.2");
"0.2"
min
import min from "preciso/min.js";
min("0.1", "0.2");
"0.1"
multiply
import multiply from "preciso/multiply.js";
multiply("-714.7008086132632", "8135.725531");
"-5814609.6156612701214627592"
multiply("2", "3", "4");
"24"
multiply(["2", "3", "4", "5"]);
"120"
multiply(["2", "-Infinity"]);
"-Infinity"
multiply(["0", "-Infinity"]);
"NaN"
multiply(["0", "-Infinity"], { infinity_times_zero: null });
null
ols
Use ordinary least squares for simple linear regression.
ols(
[
["25", "651"],
["28", "762"],
["35", "853"],
["40", "1062"],
["46", "1190"],
["53", "1293"]
],
{ max_decimal_digits: 5 }
);
{
m: "23.41401",
b: "82.66978"
}
pow (power)
import pow from "preciso/pow.js";
pow("2", "3")
"8"
pow("-2", "-3")
"-0.125"
pow("8", "1/3")
"0.5"
pow("0", "0", { zero_to_the_power_of_zero: undefined })
undefined
reciprocal
Calculate reciprocal or multiplicative inverse
import reciprocal from "preciso/reciprocal.js"
reciprocal("0.1");
"10"
reciprocal("3/4", { fraction: true })
"4/3"
remainder
import remainder from "preciso/truncate.js";
remainder("10", "3");
"1"
remainder("-0.5", "2");
"-0.5"
root
Find the n-th root of a number
import root from "preciso/root.js";
root("4096", "4");
root("1024", "7", { max_decimal_digits: 50 })
'2.69180038526471226388566520750188380873249405355495'
root("-4", "2");
round
import round from "preciso/round.js";
round("0.99")
"1"
round("0.12345");
"0"
round("0.12345", { digits: 2 })
"0.12"
sign
import sign from "preciso/subtract.js";
sign("13e451");
"+"
sign("-0.123");
"-"
sign("-0.00");
""
sort
import sort from "preciso/sort.js"
sort(["1", "2", "3"])
["1", "2", "3"]
sort(["1", "2", "3"], { direction: "descending" })
["3", "2", "1"]
softmax
Calculate the softmax function
import softmax from "preciso/softmax.js";
softmax(["1", "2", "3", "4", "1", "2", "3"], { max_decimal_digits: 8 });
["0.02364054", "0.06426166", "0.1746813", "0.474833", "0.02364054", "0.06426166", "0.1746813"]
square root
import square_root from "preciso/square_root.js";
square_root("0.25");
square_root("16");
square_root("-49");
subtract
import subtract from "preciso/subtract.js";
subtract("10", "2.14");
"7.86"
subtract("Infinity", "Infinity");
"NaN"
subtract("Infinity", "Infinity", { infinity_minus_infinity: "0" });
"0"
sum
Similar to add, but can take an array of more than two numbers.
import sum from "preciso/sum.js";
sum(["1", "2", "3"])
"6"
truncate
import truncate from "preciso/truncate.js";
truncate("-714.7008086132632");
"-714"